package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealServiceImpl setmealService;
    @Autowired
    private RedisTemplate<String, List<DishVO>> redisTemplate;


    @Transactional
    @Override
    public void addDish(DishDTO dishDTO) {
        //添加菜品信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.addDish(dish);

        //获取Dish的主键值
        Long dishId = dish.getId();

        //添加口味信息
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        if (dishFlavorList != null && !dishFlavorList.isEmpty()) {
            dishFlavorList.forEach(item -> item.setDishId(dishId));
            dishFlavorMapper.addBatch(dishFlavorList);
        }

        // 清除分类菜品Redis缓存
        redisTemplate.delete("dish_" + dish.getCategoryId());
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO, dish);
        Page<DishVO> dishVOList = dishFlavorMapper.page(dish);

        return new PageResult(dishVOList.getTotal(), dishVOList.getResult());
    }

    @Transactional
    @Override
    public void deleteDish(Long[] ids) {

        // 是否是起售中，如果是不能删除
        Integer[] statusList = dishMapper.getStatusByIds(ids);
        for (Integer status : statusList) {
            if (status.equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        // 是否有套餐关联这个菜品，如果有不能删除
        Integer count = setmealService.getDishOfSetmeal(ids);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        // 删除菜品
        dishMapper.deleteDishBatch(ids);

        // 删除口味
        dishMapper.deleteDishFlavorBatch(ids);

        // 清除分类菜品Redis缓存
        Set<Long> dishCategoryIds = new HashSet<>(Arrays.asList(ids));
        dishCategoryIds.forEach(item -> redisTemplate.delete("dish_" + item));
    }

    @Override
    public void statusDish(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.updateDish(dish);

        // 清除分类菜品Redis缓存
        Dish dish1 = dishMapper.getDish(id);
        redisTemplate.delete("dish_" + dish1.getCategoryId());
    }

    @Transactional
    @Override
    public DishVO getDish(Long id) {
        Dish dish = dishMapper.getDish(id);
        List<DishFlavor> dishFlavorList = dishFlavorMapper.getDishFlavor(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavorList);

        return dishVO;
    }

    @Transactional
    @Override
    public void updateDish(DishDTO dishDTO) {
        // 清除分类菜品Redis缓存
        Dish dish1 = dishMapper.getDish(dishDTO.getId());
        redisTemplate.delete("dish_" + dish1.getCategoryId());

        Dish dish = new Dish();
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateDish(dish);
        dishFlavorMapper.deleteFlavorByDishId(dish);
        if (dishFlavorList != null && !dishFlavorList.isEmpty()) {
            dishFlavorList.forEach(item -> item.setDishId(dish.getId()));
            dishFlavorMapper.addBatch(dishFlavorList);
        }
    }

    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getDishFlavor(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    @Override
    public List<Dish> list(Dish dish) {
        return dishMapper.list(dish);
    }


}
